package com.cl.short_video_java.web.admin;

import com.cl.short_video_java.annotation.IsRole;
import com.cl.short_video_java.annotation.Log;
import com.cl.short_video_java.annotation.Login;
import com.cl.short_video_java.common.Const;
import com.cl.short_video_java.common.Result;
import com.cl.short_video_java.mapper.VideoTypeMapper;
import com.cl.short_video_java.menum.BusinessType;
import com.cl.short_video_java.pojo.VideoType;
import com.cl.short_video_java.service.VideoTypeService;
import com.cl.short_video_java.vo.TypeSearch;
import com.cl.short_video_java.vo.type.Type;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/type")
public class TypeController {
	@Resource
	private VideoTypeService videoTypeService;
	@Resource
	private VideoTypeMapper videoTypeMapper;

	/**
	 * 分页获取所有分类信息
	 *
	 * @return
	 */
	@PostMapping("/page")
	@Login(issuer = Const.GRADE_ADMIN)
	@IsRole(menuId = 4)
	public Result SelectAllVideoType(@RequestBody TypeSearch typeSearch) {
		return Result.success(videoTypeService.getPage(typeSearch));
	}

	/***
	 * 通过分类Id查询一条数据
	 * @param id  分类Id
	 * @return
	 */
	@GetMapping("/get-one")
	@Login(issuer = Const.GRADE_ADMIN)
	@IsRole(menuId = 4)
	public Result SelectVideoTypeOneById(@RequestParam Integer id) {
		return Result.success(videoTypeService.getById(id));
	}

	/****
	 * 新增分类数据（可批量）
	 * 批量时name可传：java,c++,python这个样式的多个数据，也可只传单个，如Java
	 * @param name 分类名称
	 * @return
	 */
	@PostMapping("/insert")
	@Login(issuer = Const.GRADE_ADMIN)
	@IsRole(menuId = 4)
	@Log(title = "分类新增", businessType = BusinessType.INSERT)
	public Result InsertVideoType(@RequestParam String name) {
		return Result.success(videoTypeService.InsertVideoType(name));
	}


	/***
	 * 分类删除（可批量）
	 * 批量删除时：传入的id如下样式：1，2，3
	 * @param ids
	 * @return
	 */
	@PostMapping("/remove")
	@Login(issuer = Const.GRADE_ADMIN)
	@IsRole(menuId = 4)
	@Log(title = "分类删除", businessType = BusinessType.DELETE)
	public Result RemoveVideoType(@RequestBody List<Integer> ids) {
		return Result.success(videoTypeService.RemoveVideoTypeService(ids));
	}

	/**
	 * 分类修改
	 *
	 * @param videoType 参数
	 * @return
	 */
	@PostMapping("/updata")
	@Login(issuer = Const.GRADE_ADMIN)
	@IsRole(menuId = 4)
	@Log(title = "分类修改", businessType = BusinessType.UPDATE)
	public Result Update(@RequestBody VideoType videoType) {
		return Result.success(videoTypeMapper.updateById(videoType));
	}

	@PostMapping("/save")
	@Login(issuer = Const.GRADE_ADMIN)
	@IsRole(menuId = 4)
	@Log(title = "新增分类", businessType = BusinessType.INSERT)
	public Result Save(@RequestBody Type type) {
		videoTypeService.save(type);
		return Result.success();
	}

	/**
	 * 获取分类信息--新增时的分类选项
	 * @return
	 */
	@GetMapping("typepage")
	@Login(issuer = Const.GRADE_ADMIN)
	@IsRole(menuId = 4)
	public Result typePage(){
		return Result.success(videoTypeService.typePageService());
	}
}
